<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./js/vue.js"></script>
  </head>

  <body>
    <div id="root">
      <input type="text" placeholder="请输入" v-model="keyWord" />
      <ul>
        <!-- <li v-for="person in filPersons" :key="person.id"> -->
        <li v-for="person in filPersons1" :key="person.id">
          {{person.name}}-{{person.age}}-{{person.sex}}
        </li>
      </ul>
    </div>
    <!-- 
      1.计算属性和侦听属性都可以实现的时候优先使用计算属性
      判断字符串时候包含某个字符用indexOf(a)   不包含返回-1  包含返回0    如果a为'' 也返回0     
     -->
    <script>
      //创建Vue实例,得到 ViewModel
      const vm = new Vue({
        el: '#root',
        data: {
          keyWord: '',
          persons: [
            { id: '001', name: '马冬梅', age: 18, sex: '女' },
            { id: '002', name: '周冬雨', age: 19, sex: '女' },
            { id: '003', name: '周杰伦', age: 20, sex: '男' },
            { id: '004', name: '温兆伦', age: 21, sex: '男' },
          ],
          filPersons: [],
        },
        methods: {},
        // 列表过滤  计算属性实现 (优先使用计算属性)
        computed: {
          filPersons1() {
            return this.persons.filter((person) => {
              return person.name.indexOf(this.keyWord) !== -1
            })
          },
        },
        // 列表过滤   侦听属性实现
        watch: {
          keyWord: {
            immediate: true,
            handler() {
              this.filPersons = this.persons.filter((person) => {
                return person.name.indexOf(this.keyWord) !== -1
              })
            },
          },
        },
      })
    </script>
  </body>
</html>
